package com.zy.dp.flyweight.example3;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class SecurityMgr
{

	private static SecurityMgr securityMgr = new SecurityMgr();

	private SecurityMgr()
	{

	}

	public static SecurityMgr getInstance()
	{

		return securityMgr;
	}

	private Map<String, Collection<Flyweight>> map = new HashMap<String, Collection<Flyweight>>();

	public void login(String user)
	{
		Collection<Flyweight> col = queryByUser(user);

		map.put(user, col);
	}

	public boolean hasPermit(String user, String securityEntity, String permit)

	{

		Collection<Flyweight> col = map.get(user);
		System.out.println("现在测试" + securityEntity + "的" + permit
				+ "权限，map.size=" + map.size());
		if (col == null || col.size() == 0)
		{
			System.out.println(user + "没有登录或是没有被分配任何权限");
			return false;
		}
		for (Flyweight flyweight : col)
		{
			System.out.println(flyweight);

			if (flyweight.match(securityEntity, permit))
			{
				return true;
			}

		}

		return false;

	}

	private Collection<Flyweight> queryByUser(String user)
	{

		Collection<Flyweight> col = new ArrayList<Flyweight>();
		for (String s : TestDB.colDB)
		{
			String[] ss = s.split(",");

			if (ss[0].equals(user))
			{

				Flyweight fm = null;

				if (ss[3].equals("2"))
				{
					fm = new UnsharedConcreteFlyweight();

					String[] tempSs = TestDB.mapDB.get(ss[1]);

					for (String tempS : tempSs)
					{

						Flyweight tempFm = FlyweightFactory.getInstance()
								.getFlyweight(tempS);

						fm.add(tempFm);

					}
				}

				else
				{
					fm = FlyweightFactory.getInstance().getFlyweight(
							ss[1] + "," + ss[1]);
				}

				col.add(fm);

			}

		}

		return col;

	}
}
